<input id="hidden_goodsid" type="hidden" value="{$goods_info.goods_id}">
<input id="hidden_skuid"   type="hidden" value="">
<input id="hidden_skuname" type="hidden" value="">
<input id="hidden_sku_price" type="hidden" value="">
<input id="hidden_default_img_id" type="hidden" value="{$goods_info['img_list'][0]['pic_id']}" />
<input id="hidden_max_buy" type="hidden" value="{$goods_info.max_buy}" />
<input id="hidden_stock" type="hidden" value="{$goods_info.stock}" />
<input id="hidden_point_exchange_type" type="hidden" value="{$goods_info.point_exchange_type}"/>
<input id="hidden_point_exchange" type="hidden" value="{$goods_info.point_exchange}"/>
<input type="hidden" id="hidden_province" value="{$user_location.province}" />
<input type="hidden" id="hidden_city" value="{$user_location.city}" />
<input type="hidden" id="hidden_shipping_fee_name" value="{$goods_info.shipping_fee_name}" />
<!-- 头部右侧鼠标经过图片放大效果 _start -->
<script type="text/javascript" src="__TEMP__/{$style}/public/js/bubbleup.js"></script>
<!-- 头部右侧鼠标经过图片放大效果 _end -->
<!-- 右侧商品信息等定位切换效果 _start -->
<script type="text/javascript" src="__TEMP__/{$style}/public/js/ns_tabs_totop.js"></script>
<!-- 右侧商品信息等定位切换效果 _end -->
<!-- 放大镜 _start -->
<script type="text/javascript" src="__TEMP__/{$style}/public/js/magiczoom.js"></script>
<!-- 地址选择 _start -->
<script type="text/javascript" src="__TEMP__/{$style}/public/js/ns_select_region.js"></script>
<script type="text/javascript" src="__TEMP__/{$style}/public/js/tabs.js"></script>
<script type="text/javascript" src="__TEMP__/{$style}/public/js/goods.js"></script>
<script type="text/javascript">
var shop_main = "SHOP_MAIN";
//获取商品最小购买数量
var min_buy = parseInt($(".amount-input").attr("data-min"));
$().ready(function() {
	initSku();//加载sku
	//判断是否有总库存
	if(parseInt($("#hidden_stock").val())==0){
		$(".js-buy-now").addClass("disabled");
		$(".add-cart").addClass("disabled");
	}
// 	if($("#hidden_shipping_fee_name").val() == "无货"){
// 		$(".js-buy-now").addClass("disabled");
// 		$(".add-cart").addClass("disabled");
// 	}
	$("#num").keyup(function(){
		var obj = $(this);
		var r = /^\d+$/;
		if(!r.test(obj.val())){
			if(min_buy > 0){
				obj.val(min_buy);	
			}else{
				obj.val(1);	
			}
			$.msg("请输入数字");
		}else{
			if(parseInt(obj.val()) > parseInt(obj.attr("data-max"))){
				obj.val(obj.attr("data-max"));
			}
			//购买数不能小于最小购买数
			if(parseInt(obj.val()) < min_buy){
				obj.val(min_buy);
			}
			if(parseInt(obj.val()) <= 0){
				if(min_buy > 0){
					obj.val(min_buy);	
				}else{
					obj.val(1);	
				}
			}
		}
	})

	$(".amount-plus").click(function(){
		var obj = $("#num");
		var num = parseInt(obj.val());
		var max = parseInt(obj.attr("data-max"));
		num++;
		if(num > max){
			obj.val(max);
		}else{
			obj.val(num);
		}
	})

	$(".amount-minus").click(function(){
		var obj = $("#num");
		var num = parseInt(obj.val());
		if(num > 1){
			num--;
			if(num < min_buy){
				$.msg("本商品最小购买数为"+min_buy);
				obj.val(min_buy);
			}else{
				obj.val(num);
			}
			
		}
	})
	
	//加载规格
	function initSku(){
		var curr_sku = '';
		var sku_arr = $(".goods-spec-item");
		sku_arr.each(function(i) {
			var $this = $(this);
			if ($this.hasClass("selected")) {
				curr_sku += $this.find("span").attr("id") + ";";
			}
		});
	 	for (i = 0; i < parseInt($("#goods_sku_count").val()); i++) {
			sku_id = "#goods_sku" + i;
			goods_sku_id = $(sku_id).val();
			console.log(curr_sku+" ! "+goods_sku_id);
			if (curr_sku == goods_sku_id) {
				select_skuid = $(sku_id).attr("skuid");
				select_skuName = $(sku_id).attr("skuname");
				select_stock = $(sku_id).attr("stock");//sku商品库存
				original_price = parseFloat($(sku_id).attr("original_price")); //sku商品原价
				if(select_stock==0){
					$(".js-buy-now").addClass("disabled");
					$(".add-cart").addClass("disabled");
				}else{
					//当最小购买数大于总库存时,不可购买
					if(min_buy > select_stock){
						$(".js-buy-now").addClass("disabled");
						$(".add-cart").addClass("disabled");
					}else{
						$(".js-buy-now").removeClass("disabled");
						$(".add-cart").removeClass("disabled");						
					}
				}
				$("#hidden_skuid").val(select_skuid);
				$("#hidden_skuname").val(select_skuName);
				var price = parseFloat($(sku_id).attr("price"));
				var promotion_price = parseFloat($(sku_id).attr("promotion_price"));
				var member_price = parseFloat($(sku_id).attr("member_price"));
				$("#price").text("￥" + promotion_price.toFixed(2));
				$("#member_price").text("￥"+member_price.toFixed(2));
				if(original_price==0){
					$(".show-price").hide();
				}else{
					$(".show-price").show();
				}
				$(".market-price").text("￥" + original_price.toFixed(2));//商品原价
				$("#hidden_sku_price").val(price);
				$(".js-goods-number").text("库存"+select_stock+"件");
				if($("#hidden_max_buy").val()==0)
				{
					$(".amount-input").attr("data-max", select_stock);
				}else{
					$(".amount-input").attr("data-max", $("#hidden_max_buy").val());
				}
				//最小购买数为0时,购买数默认为1
				if(min_buy > 0){
					$(".amount-input").val(min_buy);
				}else{
					$(".amount-input").val(1);	
				}
				
				// 当只有一个sku时，不用验证
				if(parseInt($("#attribute_list").val())==1){
		 			select_specifications=true;//是否选择过规格,来源与shopping_cart.js
				}
				break;
			}
		} 
	}

	//获取sku多图的主规格id
	var spec_id = $("#hide_spec_id").val();
	if(spec_id > 0){
		var firstSku = $("[id^="+spec_id+"]").eq(0);
		firstSku.parent().parent('.goods-spec-item').addClass("selected");
		if(firstSku.attr("id") != undefined){
			var sku_id = subSkuVue(firstSku.attr("id"));
			showSkuPicture(sku_id);
		}
	}

	//截取sku属性值
	function subSkuVue(goods_sku_id){
		num = goods_sku_id.indexOf(":")+1;
		sku_vue_id = goods_sku_id.substr(num);
		return sku_vue_id;
	}

	//根据sku_id显示相册中的图片
	function showSkuPicture(sku_id){
		if($(".spec-items ul li#sku_pic_"+sku_id).length > 0){
			$(".spec-items ul li").hide();
			$(".spec-items ul li#sku_pic_"+sku_id).show();
			var firstPicUrl = $(".spec-items ul li img").not(":hidden").eq(0).attr("data_big_img");
			$(".spec-items ul li").not(":hidden").removeClass("on");
			$(".spec-items ul li").not(":hidden").eq(0).addClass("on");
			$(".MagTargetImg").attr("src",firstPicUrl);
			$(".MagTargetImg").attr("data_big_img",firstPicUrl);
		}
	}

	//规格选择
	$(".goods-spec-item").click(function() {
		if($("#hidden_shipping_fee_name").val() != "无货"){
			$(this).siblings(".selected").removeClass("selected").find("i").remove();
			$(this).addClass("selected");
			if($(this).find("a i").length==0){
				$(this).find("a").append("<i></i>");
			}
			var this_sku_vue = $(this).find("span").attr("id");
			sku_vue = subSkuVue(this_sku_vue);
			showSkuPicture(sku_vue);			
			initSku();
			//规格图片
			var show_big_pic = $(this).find("span").attr("data-show-big-pic");
			if(show_big_pic != undefined && show_big_pic != ""){
				$(".MagTargetImg").attr("src", upload+"/"+show_big_pic);
			}
		}
	}).hover(function() {
// 		$(this).addClass("selected");
	}, function() {
		if($(this).find("i").size() == 0) {
			$(this).removeClass("selected");
		}
	});
	
	// 添加购物车
	$(".add-cart,.js-buy-now").click(function(event) {
		if($(this).hasClass("disabled")) {
			return;
		}
		if($(".amount-input").val() < min_buy){
			$.msg("本商品最小购买数为"+min_buy);
			return;
		}
		var tag = $(this).attr("data-tag");
		var image_url = $("#MagnifierWrap .MagnifierMain").find("img").attr("src");
		var goodsid = {$goods_info.goods_id};
		 $.cart.add(goodsid, $(".amount-input").val(), {
			is_sku: true,
			image_url: image_url,
			event: event,
			tag : tag
		});
	});

	// 添加对比
	$(".add-compare").click(function(event) {
		var target = $(this);
		var goods_id = $(this).data("goods-id");
		var sku_id = $(this).add("sku-id");
		var image_url = $(this).data("image-url");
		$.compare.toggle(goods_id, image_url, event, function(result) {
			if(result.data == 1) {
				$(target).addClass("curr");
			} else {
				$(target).removeClass("curr");
			}
		});
	});

	var is_member_fav_goods = {$is_member_fav_goods};
	// 添加收藏
	$(".collect-goods").click(function(event) {
		var num = $(this).find("span").attr("data-collects");
		var obj = $(this).find("span");
		if(is_member_fav_goods==0){
			//点击收藏
			$.ajax({
				url: "{:__URL('SHOP_MAIN/components/collectiongoodsorshop')}",
				type: "post",
				data: { "fav_id":{$goods_info.goods_id},"fav_type":"goods","log_msg":"{$goods_info.goods_name}"},
				success: function (data) {
					if(data.code>0){
						is_member_fav_goods = 1;
						num++;
						obj.attr("data-collects",num);
						obj.text("收藏商品 ("+num+"人气)");
					}else{
						$.msg("您已经收藏该商品");
					}
					$(".collect-goods").find("i").css("background-position","-112px -91px");
				}
			});
		}else if (is_member_fav_goods== 1){
			//取消收藏
			$.ajax({
				url: "{:__URL('SHOP_MAIN/components/cancelcollgoodsorShop')}",
				type: "post",
				data: { "fav_id":{$goods_info.goods_id},"fav_type":"goods","log_msg":"{$goods_info.goods_name}"},
				success: function (data) {
					if(data.code>0){
						num--;
						obj.attr("data-collects",num);
						obj.text("收藏商品 ("+num+"人气)");
						$(".collect-goods").find("i").css("background-position","-112px -115px");
						is_member_fav_goods = 0;
					}else{
						$.msg("您已经取消收藏该商品");
					}
				}
			});
		}else{
			location.href=__URL("SHOP_MAIN/login/index");
		}
	});
	
	//根据地址计算邮费，商品详情中使用
// 	$(".region-items[data-region-level='2']").click(function(){
// 		getExpressFee($('.js-region').attr("data-province"),$('.js-region').attr("data-city"),$('.js-region').attr("data-district"));
// 	})
	
// 	//根据地址查询运费
// 	function getExpressFee(province_id,city_id,district_id){
// 		if(city_id == null || city_id == '' || province_id == null || province_id == ''){
// 			return;
// 		}
// 		$.ajax({
// 			url : "{:__URL('SHOP_MAIN/goods/getexpressfee')}",
// 			type : "post",
// 			data : { "goods_id" : $("#hidden_goodsid").val(), "province_id" : province_id, "city_id" : city_id, "district_id" : district_id },
// 			success : function(res){
// 				if(res == "无货"){
// 					$(".js-buy-now").addClass("disabled");
// 					$(".add-cart").addClass("disabled");
// 				}else{
// 					$(".js-buy-now").removeClass("disabled");
// 					$(".add-cart").removeClass("disabled");
// 				}
// 				$(".freight-info").text("运费："+res);
// 			}
// 		})
// 	}

});
</script>